﻿using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class TaoJinDi : Information
    {
        string name, pass;
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("beforUrl", "");
            pd.Add("member.loginName", name);
            pd.Add("member.password", pass);
            pd.Add("checkcode", "输入验证码");

            string html = source.Post("http://www.taojindi.com/memberLogin.do", pd.Read(), "http://www.taojindi.com/memberLogin.do");
            if (html.IndexOf("<a href=\"memberLogout.do\" class=\"blue\">安全退出</a>") != -1)
            {
                this.name = name;
                this.pass = pass;
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult info = new InformationResult();
            string html = source.Get("http://www.taojindi.com/member/product/editProduct.do?product.dealType=SELL&product.infoType=PRODUCT");
            string pic_html = UploadImages(Regex.Match(html, "name=\"album.id\" value=\"([0-9]+)\"").Groups[1].Value);

            ParametersData pd = new ParametersData("gb2312");
            pd.Add("pageTag", "");
            pd.Add("product.id", "");
            pd.Add("product.lastupdate", "");
            pd.Add("product.favoriteCount", "0");
            pd.Add("product.clickCount", "0");
            pd.Add("product.createTime", "");
            pd.Add("product.mainPicture", "img1/pho" + Regex.Match(pic_html, "largePath\":\"http://img1\\.taojindi\\.com/pho/L(/[^\"]+)").Groups[1].Value);
            pd.Add("product.pictureIds", "1," + Regex.Match(pic_html, "\"title\":\"([0-9]+)\",\"waterPath").Groups[1].Value);
            pd.Add("product.pictureIds", "");
            pd.Add("product.pictureIds", "");
            pd.Add("product.status", "PENDING");
            pd.Add("product.dealType", "SELL");
            pd.Add("uploadPicIds", "");
            pd.Add("uploadPicIds", "");
            pd.Add("uploadPicIds", "");
            pd.Add("memberGrade", "1");
            pd.Add("uploadFiles", "");
            pd.Add("product.infoType", "PRODUCT");
            pd.Add("product.metaKeyword", "供应" + title);
            pd.Add("product.name", "供应" + title);
            pd.Add("product.catId", site.TypeId[0]);
            pd.Add("product.metaDescr", "");
            pd.Add("product.descr", content);
            pd.Add("product.validDays", "183");
            pd.Add("a16263", pif.Get("产品名称"));
            pd.Add("a16264", pif.Get("产品名称"));
            pd.Add("a16265", "");
            pd.Add("a16266", "");
            pd.Add("a16267", "");
            pd.Add("a56137", pif.Get("单位"));
            pd.Add("a56138", "1");
            pd.Add("a56139", "");
            pd.Add("a56139", "");
            pd.Add("product.contactIds", Regex.Match(html, "name=\"product.contactIds\" value=\"([0-9]+)\"").Groups[1].Value);
            pd.Add("__multiselect_product.contactIds", "");

            html = source.Post("http://www.taojindi.com/member/product/addProduct.do", pd.Read(), "http://www.taojindi.com/member/product/editProduct.do?product.dealType=SELL&product.infoType=PRODUCT");

            if (html.IndexOf("发布成功，您的信息已提交审核") != -1)
            {
                pd = new ParametersData();
                pd.Add("pageBean.currentPage", "1");
                pd.Add("pageBean.totalPage", "1");
                pd.Add("pageBean.pageSize", "10");
                pd.Add("pageBean.allRow", "1");
                pd.Add("product.dealType", "SELL");
                pd.Add("orderWay", "desc");
                html = source.Post("http://www.taojindi.com/member/product/listPendingProduct.do", pd.Read(), "http://www.taojindi.com/member/product/listPendingProduct.do");
                info.Code = 1;
                info.Content = "http://www.taojindi.com/product/" + Regex.Match(html, "<a href=\"editProduct\\.do\\?product\\.dealType=SELL&product\\.id=([0-9]+)&pageTag=edit\" target=\"_blank\">供应" + title + "</a>").Groups[1].Value + ".html";
            }
            else if (html.IndexOf("您还没登录，请先登录吧！") != -1)
            {
                if (Login(name, pass, ""))
                {
                    info.Content = "退出登陆,从新登陆成功";
                }
                else
                {
                    info.Content = "退出登陆,从新登陆失败";
                }
            }
            return info;
        }

        private string UploadImages(string pid)
        {
            SourceFormData sfd = new SourceFormData("utf-8");
            //头部数据
            sfd.Write("album.id", pid);
            sfd.Write("prodPhotoOrder", "1");
            //发送文件
            sfd.WriteFile("shopPhoto", RandomPicture.Get());
            //尾部数据
            sfd.Write("waterMark", "true");
            //结尾
            string html = source.Post("http://www.taojindi.com/member/shop/ajaxUploadProductPhoto.do", sfd.GetContentType(), sfd.Read(), "http://www.taojindi.com/member/product/editProduct.do?product.dealType=SELL&product.infoType=PRODUCT");
            return html;
        }
    }
}
